//
//  TMTabBarViewController.m
//  TMFishTravel
//
//  Created by Playboy on 2017/10/11.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "TMTabBarViewController.h"
#import "TMNavViewController.h"
#import "PBHomeViewController.h"
#import "PBDeputeViewController.h"
#import "PBCollectViewController.h"
#import "PBMyViewController.h"

@interface TMTabBarViewController ()<UITabBarControllerDelegate,UITabBarDelegate>
{
    TMNavViewController *_homeNav;
}
@end

@implementation TMTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configureView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationItem.hidesBackButton = YES;
}

- (void)configureView {
    
    self.delegate = self;

    
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColorWithRGB(51, 51, 51),NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    UIColor *titleHighlightedColor = UIColorWithRGB(243,159,13);

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleHighlightedColor, NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];
    
    // 首页
    PBHomeViewController *homeVC = [[PBHomeViewController alloc] init];
    TMNavViewController *homeNav = [[TMNavViewController alloc] initWithRootViewController:homeVC];
    UITabBarItem *homeTabBar = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"lab_icon_one"] selectedImage:[UIImage imageNamed:@"lab_icon_one_selected"]];
    homeNav.tabBarItem = homeTabBar;
    _homeNav = homeNav;
    [TMShareData shareData].homeNav = homeNav;

    // 房屋委托
    PBDeputeViewController *deputeVC = [[PBDeputeViewController alloc] init];
    TMNavViewController *deputeNav = [[TMNavViewController alloc] initWithRootViewController:deputeVC];
    UITabBarItem *orderTabBar = [[UITabBarItem alloc] initWithTitle:@"房屋委托" image:[UIImage imageNamed:@"lab_icon_two"] selectedImage:[UIImage imageNamed:@"lab_icon_two_selected"]];
    deputeNav.tabBarItem = orderTabBar;

    // 收藏
    PBCollectViewController *collectVC = [[PBCollectViewController alloc] init];
    TMNavViewController *collectNav = [[TMNavViewController alloc] initWithRootViewController:collectVC];
    UITabBarItem *collectTabBar = [[UITabBarItem alloc] initWithTitle:@"收藏" image:[UIImage imageNamed:@"lab_icon_three"] selectedImage:[UIImage imageNamed:@"lab_icon_three_selected"]];
    collectNav.tabBarItem = collectTabBar;
    
    // 我的
    PBMyViewController *myVC = [[PBMyViewController alloc] init];
    TMNavViewController *myNav = [[TMNavViewController alloc] initWithRootViewController:myVC];
    UITabBarItem *myTabBar = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"lab_icon_four"] selectedImage:[UIImage imageNamed:@"lab_icon_four_selected"]];
    myNav.tabBarItem = myTabBar;
    [TMShareData shareData].myNav = myNav;
    self.viewControllers = @[homeNav,deputeNav,collectNav,myNav];
    
}


#pragma mark - <UITabBarControllerDelegate,UITabBarDelegate>
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    NSInteger index=[tabBarController.viewControllers indexOfObject:viewController];

    if (index == 0) {
        [_homeNav popToRootViewControllerAnimated:YES];
    }
    
    return YES;
}

- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
